package at.irian.jsfatwork.broker.web.model;

import at.irian.jsfatwork.broker.domain.account.Accounting;
import at.irian.jsfatwork.broker.domain.account.ClearingAccount;
import at.irian.jsfatwork.broker.domain.account.SecuritiesAccount;
import at.irian.jsfatwork.broker.domain.client.Client;
import at.irian.jsfatwork.broker.domain.stock.StockExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author Thomas Spiegl
 */
@Component
@Scope("conversation.manual")
public class AccountsBean {

    @Autowired
    private Accounting accounting;

    @Autowired
    private ClientBean clientBean;

    @Autowired
    private StockExchange stockExchange;

    private SecuritiesAccount securitiesAccount;

    private ClearingAccount clearingAccount;

    // ----------------------------- Bindings

    public SecuritiesAccount getSecuritiesAccount() {
        if (securitiesAccount == null) {
            Client client = clientBean.getClient();
            securitiesAccount = accounting.getClientSecuritiesAccount(client);
        }
        return securitiesAccount;
    }

    public ClearingAccount getClearingAccount() {
        if (clearingAccount == null) {
            Client client = clientBean.getClient();
            clearingAccount = accounting.getClientClearingAccount(client);
        }
        return clearingAccount;
    }

    public BigDecimal getSecuritiesAccountBalance() {
        return getSecuritiesAccount().getSecuritiesAccountBalance(stockExchange);

    }



    // ----------------------------- Actions

    // ----------------------------- Misc



}